package cn.tlb.ui.resource.drawable;

import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import cn.tlb.upload.Form;
import cn.tlb.upload.FormUtil;
import cn.tlb.upload.Input;

/**
 * Created by 汤林冰 on 2016/1/7 13:48.
 */
public class UploadHelper {
    private static final String TAG = UploadHelper.class.getSimpleName();
    private static final String PIC_DIR = Environment.getExternalStorageDirectory() + "/Pictures/Screenshots";
    private static final String FILENAME = PIC_DIR + "/" + new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()) + ".png";
    private static final String FILENAME_JPG = PIC_DIR + "/" + new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()) + ".jpg";
    private static final String LOCAL_HOST = "http://192.168.47.146:8080/http/servlet/FileUploadServlet";

    public static void uploadFile(File file) {
        uploadFile(file.getAbsolutePath());
    }

    public static void uploadFile(String path) {
        Form form = new Form(LOCAL_HOST, "POST");
        form.inputs.add(Input.newFileInput("file", path));
        new FormUtil().uploadMultipartForm(form, new MyListener());
    }

    private static class MyListener implements FormUtil.OnSubmitListener {

        @Override
        public void onStart() {

        }

        @Override
        public void onError(Throwable th) {
            Log.i(TAG, "onError--->" + th);
        }

        @Override
        public void onUploading(String absFilePath, long totalSize, long uploadSize, int progress) {

        }

        @Override
        public void onCancel() {

        }

        @Override
        public void onSuccess(String result) {
            Log.i(TAG, "onSuccess--->" + result);
        }

        @Override
        public void onFailed(int responseCode) {

        }

        @Override
        public void onFinish() {

        }
    }

}
